<?php

defined('BASEPATH') or die('forbidden');

class BlackList_Controller Extends Controller {

    private $wholeServerHost;
    private $guide;
    private $server_info;
    private $areaList;
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $config = get_config();

        $this->wholeServerHost = $config['host_url'];
        $this->guide = $config['guide'];

        //query server mapping
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1'";
        $this->db->query($selectSql);
        $this->server_info=$this->db->row('default');

        $server_list=get_server_list();
        foreach($server_list as $k=>$row) {
            $this->areaList[$row['SvrOutAddr']] = $row['SvrID']."-".$row['SvrName'];
        }
        // db数据库
        //$this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");
    }

    function index() {
        $this->view->display("seal/blacklist/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $result = array(
            'page' => $page,
            'total' => 0,
            'rows' => null
        );
        $str = $this->wholeServerHost . "action=48";
        
        $strResult = vpost($str);
        if ($strResult != '0' || $strResult === FALSE)
            $result = 1;
        $row = json_decode($strResult, true);

        if ($row != null) {
            foreach ($row as $key => $value) {
                //if ($value['endtime'] <= time())
                //    continue;
                $tmpkind = $value['kind'];
                $value['kind'] = $this->kindArray[$value['kind']];
                $value['endtime'] = $value['endtime']==0 ? translate("FOREVER"):get_datetime($value['endtime']);
                $rows[] = array('id' => $tmpkind."_".$value['data'], 'cell' => $value);
            }
            $total = count($rows);
            $result = array(
                'page' => $page,
                'total' => $total,
                'rows' => $rows
            );
        }

        echo json_encode($result);
    }

    function add() {

        if (is_submit()) {
            $rowdata = $_POST['row'];
            //var_dump($rowdata);
            $db=$this->server_info[$rowdata['timelength']];
            // db数据库
            $m = new MongoClient("mongodb://{$db['username']}:{$db['userpwd']}@{$db['dbip']}");
            $collection=$m->{$db['dbname']}->PlayerRole;
            $filter=array('_id' => (float)$rowdata['reason']);
            $document=$collection->findOne($filter);
            //var_dump($document);
            $guide=json_decode($document['newwizard'],true);
            //var_dump($guide);
            if($guide['default'] != null){
                $key=array_search($guide['default'],$this->guide);
                $guide['default']=$this->guide[$key-1];
                $post_data['PlayerID']=(float)$rowdata['reason'];
                $post_data['SessionID']='ewewqw';
                $post_data['SessionKey']='wqqew';
                $post_data['NewWizard']=addslashes(json_encode($guide));
//                var_dump($rowdata['kind']);
//                var_dump($post_data);
//                var_dump(str_replace("\\\\\\", "\\", json_encode($post_data)));
                $strResult=vpost($rowdata['kind'].'/gm_set_new_wizard',3,str_replace("\\\\\\", "\\", json_encode($post_data)));
                $result=json_decode($strResult,true);
//                var_dump($strResult);
                //操作失败或者连接失败
                if($result['RetCode'] == 0)
                {
                    cp_tips('success');
                }
                else
                {
                    unset($rowdata);
                    cp_tips('error');
                }

            }else{
                cp_tips('guide is null');
            }

        }else 
        {
	        $this->view->assign("timelist",$this->server_info);
	        $this->view->assign("kindlist", $this->areaList);
	        $this->view->display("seal/blacklist/add");
        }
    }

    function del($id = 0) {
        $arr = explode("_", $id);
        $str = $this->wholeServerHost . "kind={$arr[0]}&data={$arr[1]}&action=50&id={$id}";

        $strResult = vpost($str);

        $res = (int) $strResult == 0 ? 1 : 0;
        if ($res == 1) {
            insert_log(translate('MENU_SEAL_BLACKLIST'),translate('DEL'), array('id'=>$id));
        }
        json_output($res, translate('C_TIPS_ERROR_TEXT'));
    }

}